package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.activity.service.IFlowService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessFlag;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.FyProjectApply;
import com.ruoyi.system.domain.FyProjectApplyAncillary;
import com.ruoyi.system.domain.WcChangePatentApplication;
import com.ruoyi.system.domain.WcChangePatentApplicationAncillary;
import com.ruoyi.system.domain.vo.business.*;
import com.ruoyi.system.mapper.WcChangePatentApplicationMapper;
import com.ruoyi.system.service.IWcChangePatentApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 剧本变更备案申请Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@Service
public class WcChangePatentApplicationServiceImpl extends ServiceImpl<WcChangePatentApplicationMapper, WcChangePatentApplication> implements IWcChangePatentApplicationService
{
    @Autowired
    private WcChangePatentApplicationMapper wcChangePatentApplicationMapper;
    @Autowired
    private WcChangePatentApplicationAncillaryServiceImpl ancillaryService;
    @Autowired
    private IFlowService flowServiceImpl;
    /**
     * 查询剧本变更备案申请
     *
     * @param id 剧本变更备案申请主键
     * @return 剧本变更备案申请
     */
    @Override
    public WcChangePatentApplication selectWcChangePatentApplicationById(Long id)
    {
        return wcChangePatentApplicationMapper.selectWcChangePatentApplicationById(id);
    }

    /**
     * 查询剧本变更备案申请列表
     *
     * @param wcChangePatentApplication 剧本变更备案申请
     * @return 剧本变更备案申请
     */
    @Override
    public List<WcChangePatentApplication> selectWcChangePatentApplicationList(WcChangePatentApplication wcChangePatentApplication)
    {
        return wcChangePatentApplicationMapper.selectWcChangePatentApplicationList(wcChangePatentApplication);
    }

    /**
     * 新增剧本变更备案申请
     *
     * @param wcChangePatentApplication 剧本变更备案申请
     * @return 结果
     */
    @Override
    public int insertWcChangePatentApplication(WcChangePatentApplication wcChangePatentApplication)
    {
        wcChangePatentApplication.setCreateTime(DateUtils.getNowLocalDateTime());
        return wcChangePatentApplicationMapper.insertWcChangePatentApplication(wcChangePatentApplication);
    }

    /**
     * 修改剧本变更备案申请
     *
     * @param wcChangePatentApplication 剧本变更备案申请
     * @return 结果
     */
    @Override
    public int updateWcChangePatentApplication(WcChangePatentApplication wcChangePatentApplication)
    {
        wcChangePatentApplication.setUpdateTime(DateUtils.getNowLocalDateTime());
        return wcChangePatentApplicationMapper.updateWcChangePatentApplication(wcChangePatentApplication);
    }

    /**
     * 批量删除剧本变更备案申请
     *
     * @param ids 需要删除的剧本变更备案申请主键
     * @return 结果
     */
    @Override
    public int deleteWcChangePatentApplicationByIds(String ids)
    {
        return wcChangePatentApplicationMapper.deleteWcChangePatentApplicationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除剧本备案申请信息
     *
     * @param id 剧本备案申请主键
     * @return 结果
     */
    @Override
    public int deleteWcChangePatentApplicationById(Long id) {
        return wcChangePatentApplicationMapper.deleteWcChangePatentApplicationById(id);
    }

    @Override
    public Page<WcChangePatentApplicationSelectListVO> list(WcChangePatentApplicationSelectListDto dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setApprovalUserId(userId);
        Page<WcChangePatentApplicationSelectListVO> page = new Page<>();
        page.setSize(dto.getPageSize());
        page.setCurrent(dto.getPageNum());
        wcChangePatentApplicationMapper.getList(page, dto);
        return page;
    }

    @Override
    public WcChangePatentApplicationDetailVO selectWcChangePatentApplicationDetailVOById(Long id) {
        return wcChangePatentApplicationMapper.selectWcChangePatentApplicationDetailVOById(id);
    }

    @Override
    public WcChangePatentApplicationSaveVO selectDetailVOById(Long id) {
        return wcChangePatentApplicationMapper.selectDetailVOById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdateSave(WcChangePatentApplicationSaveVO saveVO, Long id) throws Exception {
        if (StringUtils.isEmpty(saveVO.getFlowId())){
            saveVO.setFlowId(null);
        }
        WcChangePatentApplication entity = new WcChangePatentApplication();
        BeanUtils.copyBeanProp(entity, saveVO);
        if (id == null || id == 0) {
            entity.setFlowId(null);
            entity.setId(null);
            this.save(entity);
            if ("0".equals(saveVO.getIsOutbox())) {
                // 非草稿箱
                flowServiceImpl.start(entity.getId(), BusinessFlag.WC_CHANGE_PATENT.getValue());
            }
        }else {
            String isOutboxOld = this.getById(id).getIsOutbox();
            if ("1".equals(saveVO.getIsOutbox())) {
                entity.setFlowId(null);
            }
            this.updateById(entity);
            if ("1".equals(isOutboxOld) && "0".equals(saveVO.getIsOutbox())) {
                // 之前为草稿箱 保存为非草稿箱
                String processInstanceId = flowServiceImpl.start(entity.getId(), BusinessFlag.WC_CHANGE_PATENT.getValue());
                entity.setFlowId(processInstanceId);
            }

        }

        WcChangePatentApplicationAncillary ancillary = new WcChangePatentApplicationAncillary();
        BeanUtils.copyBeanProp(ancillary, saveVO);
        ancillary.setWcChangePatentApplicationId(entity.getId());
        if (id == null || id == 0) {
            ancillaryService.save(ancillary);
        }else {
            UpdateWrapper<WcChangePatentApplicationAncillary> wrapper = new UpdateWrapper<>();
            wrapper.eq("wc_change_patent_application_id",ancillary.getWcChangePatentApplicationId());
            ancillaryService.saveOrUpdate(ancillary,wrapper);
        }

    }
}
